/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   配置ATIM为主设备， BTIM2为从设备。BTIM2统计ATIM更新的次数。
  *          设置 SysClk = HCLK = PCLK = HSI8MHz
  *          配置ATIM 为主设备：8分频，重载值为1000-1，则溢出周期为1mS
  *          配置BTIM2为从设备：1分频，重载值为100-1，计数器模式对ATIM_TRGO信号进行计数
  *          PB03作为BTIM2的TOGN输出，用来指示BTIM2的工作状态, 每100mS翻转一次
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：断开J22
  *                    请用用示波器观察PB03的波形
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_hal_btim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void BTIM2_Init(void);
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI8MHZ);

    // 初始化SysTick
    HAL_InitTick(8000000);

    // 初始化BTIM2
    BTIM2_Init();

    // 初始化ATIM
    ATIM_Init();

    while (1)
    {
        ;
    }
}


/*******************************************************************************
 * @brief  BTIM2初始化
 * @param  None
 * @retval None
 */
static void BTIM2_Init(void)
{
    GPIO_InitTypeDef      GPIO_InitStruct;
    BTIM_InitTypeDef      BTIM_InitStruct;

    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));
    MemClr(&BTIM_InitStruct, sizeof(BTIM_InitStruct));

    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_BTIM_CLK_ENABLE();

    AFx_BTIM2TOGN_PB03();
    GPIO_InitStruct.Pins = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    // 配置BTIM
    BTIM_InitStruct.WorkMode  = BTIM_WORKMODE_COUNTER;                // 计数模式
    BTIM_InitStruct.OneShotEn = BTIM_ONESHOT_DISABLE;
    BTIM_InitStruct.Prescaler = 1 - 1;
    BTIM_InitStruct.Reload    = 100 - 1;                              // 每计数100次翻转一次IO
    HAL_BTIM_Init(SYM_BTIM2, &BTIM_InitStruct);

    // 配置BTIM的触发输入源为ATIM_TRGO
    HAL_BTIM_TriggerSource_Config(SYM_BTIM2, BTIM_TRGISRC_ATIM_TRGO, BTIM_TRGIPOL_POSITIVE, BTIM_TRGIFLT_NONE);

    // 配置BTIM的TOG输出
    HAL_BTIM_TOG_Enable(SYM_BTIM2);

    HAL_BTIM_Start(SYM_BTIM2);
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    ATIM_MasterConfigTypeDef ATIM_MasterCfgStruct;
    MemClr(&ATIM_MasterCfgStruct, sizeof(ATIM_MasterCfgStruct));

    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 8 - 1;
    hatim.Init.Period            = 1000 - 1;                          // 溢出周期1ms
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_Base_Init(&hatim);

    ATIM_MasterCfgStruct.MasterOutputTrigger = ATIM_TRGO_UPDATE;      // 配置TRGO的输出为UPDATE
    ATIM_MasterCfgStruct.MasterSlaveMode     = ATIM_MASTERSLAVEMODE_ENABLE;
    HAL_ATIM_MasterConfigSynchronization(&hatim, &ATIM_MasterCfgStruct);

    HAL_ATIM_Base_Start(&hatim);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
